package com.rc.saas.tenant.service.sms;


import com.rc.saas.tenant.mapper.sms.TenantSmsMapper;
import com.rc.saas.tenant.mapper.sms.TenantSmsRecordMapper;
import com.rc.saas.tenant.model.sms.TenantSms;
import com.rc.saas.tenant.model.sms.TenantSmsExample;
import com.rc.saas.tenant.model.sms.TenantSmsRecord;
import com.rc.saas.tenant.model.tenant.Tenant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TenantSmsServiceImpl implements TenantSmsService {
    @Autowired
    private TenantSmsMapper tenantSmsMapper;
    @Autowired
    private TenantSmsRecordMapper tenantSmsRecordMapper;

    @Override
    public TenantSmsMapper getMapper() {
        return tenantSmsMapper;
    }

    @Override
    public synchronized void updateTenantSmsByTenantCode(String phone, String tenantCode, Long memberId, String recordType, String content, String remark) {
        //更新表TenantSms 剩余量
        tenantSmsMapper.updateByTenantCode(tenantCode);
        //更新表TenantSmsRecord
        TenantSmsRecord tenantSmsRecord = new TenantSmsRecord();
        tenantSmsRecord.setTenantCode(tenantCode);

        tenantSmsRecord.setRecordType(recordType);
        tenantSmsRecord.setContent(content);
        tenantSmsRecord.setRemark(remark);
        tenantSmsRecord.setMobile(phone);
        tenantSmsRecordMapper.insert(tenantSmsRecord);
    }

    @Override
    public synchronized void updateTenantSmsByTenantCode(String tenantCode, String mobile, String recordType, String content, String remark) {
        //更新表TenantSms 剩余量
        tenantSmsMapper.updateByTenantCode(tenantCode);
        //更新表TenantSmsRecord
        TenantSmsRecord tenantSmsRecord = new TenantSmsRecord();
        tenantSmsRecord.setTenantCode(tenantCode);
        tenantSmsRecord.setMobile(mobile);
        tenantSmsRecord.setRecordType(recordType);
        tenantSmsRecord.setContent(content);
        tenantSmsRecord.setRemark(remark);
        tenantSmsRecordMapper.insert(tenantSmsRecord);
    }

    @Override
    public TenantSms selectByTenantCode(String tenantCode) {
        TenantSmsExample tenantSmsExample = new TenantSmsExample();
        tenantSmsExample.createCriteria().andTenantCodeEqualTo(tenantCode);
        List<TenantSms> tenantSms = tenantSmsMapper.selectByExample(tenantSmsExample);
        if (tenantSms != null && tenantSms.size() > 0) {
            return tenantSms.get(0);
        }
        return null;
    }
}
